﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.Timers;
using FrankensteamClient.Tabs;

namespace FrankensteamClient
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public ProjectsTab _projectsTab = new ProjectsTab();
        public UploadTab _uploadTab = new UploadTab();
        public FriendsTab _friendsTab = new FriendsTab();
        public ChatTab _chatTab = new ChatTab();
        public SettingsTab _settingsTab = new SettingsTab();

        private string _fileName;

        //SINGLETON
        private static Window _mainWindowInstance = null;

        public static Window MainWindowInstance
        {
            get
            {
                if (_mainWindowInstance == null)
                {
                    return new MainWindow();
                }
                return _mainWindowInstance;
            }
        }

        public MainWindow()
        {
            InitializeComponent();
            _mainWindowInstance = this;
        }

        private void MinimiseWindow_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void CloseWindow_Click(object sender, RoutedEventArgs e)
        {
            ClientAPI.Model.Instance.LogOut();
            this.Close();
        }

        private void MaximiseWindow_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Maximized;
            Button MaximiseButton = (Button)sender;
            MaximiseButton.Click -= new RoutedEventHandler(NormaliseWindow_Click); //set it to null from last usage
            MaximiseButton.Content = "o";
            MaximiseButton.Click += new RoutedEventHandler(NormaliseWindow_Click);
        }

        private void NormaliseWindow_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Normal;
            Button MaximiseButton = (Button)sender;
            MaximiseButton.Click -= new RoutedEventHandler(NormaliseWindow_Click); //set it to null from last usage
            MaximiseButton.Content = "O";
            MaximiseButton.Click += new RoutedEventHandler(MaximiseWindow_Click);
        }

        private void MenuBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void SearchProj_Click(object sender, RoutedEventArgs e)
        {
            string[] temp = ClientAPI.Model.Instance.GetProjectSearch(txtSearchProj.Text);

            lstBoxProjects.Items.Clear();
            if (!lstBoxProjects.Dispatcher.CheckAccess())
            {
                lstBoxProjects.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate()
                {
                    foreach (string i in temp)
                    {
                        lstBoxProjects.Items.Add(i);
                    }
                })
                );
            }
            else
            {
                foreach (string i in temp)
                {
                    lstBoxProjects.Items.Add(i);
                }
            }
        }

        private void ViewProj_Click(object sender, RoutedEventArgs e)
        {
            DisplayMessage("VIEW PROJECT");
        }

        private void DownloadProj_Click(object sender, RoutedEventArgs e)
        {
            ClientAPI.Model.Instance.Download(lstBoxProjects.SelectedItem.ToString());
        }

        private void UploadButton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.FileName = "Project"; // Default file name
            dlg.DefaultExt = ".txt"; // Default file extension
            dlg.ShowDialog();
            _fileName = dlg.FileName;
        }

        private void UploadStatus_Click(object sender, RoutedEventArgs e)
        {
            if (_fileName == "" || _fileName == null)
            {
                MessageTextBox("Error", "Please select a file", true);
            }
            else
            {
                ClientAPI.Model.Instance.SendFile(_fileName, txtBoxProjectName.Text, txtVersion.Text, txtSpecialThanks.Text, txtBoxProjectInfo.Text);
                _fileName = "";
            }
        }

        private void SignOut_Click(object sender, RoutedEventArgs e)
        {
            LoginWindow LW = new LoginWindow();
            ClientAPI.Model.Instance.LogOut();
            LW.Show();
            this.Close();
        }

        private void Reboot_Click(object sender, RoutedEventArgs e)
        {
            DisplayMessage("REBOOT");

        }

        private void DisplayMessage(string messageBoxText)
        {
            string caption = "FRANKENSTEAM";
            MessageBoxButton button = MessageBoxButton.OKCancel;
            MessageBoxImage icon = MessageBoxImage.Question;
            MessageBox.Show(messageBoxText, caption, button, icon);

        }

        private void btnStartChat_Click(object sender, RoutedEventArgs e)
        {
            _chatTab.OpenChatWindow();
        }

        private void MessageTextBox(string text, string caption, bool warning)
        {
            MessageBoxImage icon;
            MessageBoxButton button = MessageBoxButton.OKCancel;

            if (warning)
                icon = MessageBoxImage.Error;
            else
                icon = MessageBoxImage.Information;

            MessageBox.Show(text, caption, button, icon);
        }

        private void lstBoxProjects_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {            
            if (!listBoxProjectInfo.Dispatcher.CheckAccess())
            {
                listBoxProjectInfo.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate()
                {
                    listBoxProjectInfo.Text = "";
                    string[] temp = ClientAPI.Model.Instance.GetProjectInfo(lstBoxProjects.SelectedItem.ToString());
                    foreach (string i in temp)
                    {
                        listBoxProjectInfo.Text += i + Environment.NewLine + Environment.NewLine;
                    }
                })
                );
            }
            else
            {
                listBoxProjectInfo.Text = "";
                if (lstBoxProjects.SelectedItem != null)
                {
                    string[] temp = ClientAPI.Model.Instance.GetProjectInfo(lstBoxProjects.SelectedItem.ToString());

                    foreach (string i in temp)
                    {
                        listBoxProjectInfo.Text += i + Environment.NewLine;
                    }
                }
            }
        }

        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {
            txtSearchProj.Text = "";
            string[] temp = ClientAPI.Model.Instance.GetProjectsList();

            lstBoxProjects.Items.Clear();
            if (!lstBoxProjects.Dispatcher.CheckAccess())
            {
                lstBoxProjects.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate()
                {
                    foreach (string i in temp)
                    {
                        lstBoxProjects.Items.Add(i);
                    }
                })
                );
            }
            else
            {
                foreach (string i in temp)
                {
                    lstBoxProjects.Items.Add(i);
                }
            }
        }
    }
}